package com.viphuli.fragment;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import me.iwf.photopicker.PhotoPickerActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import butterknife.Bind;
import butterknife.OnClick;

import com.loopj.android.http.RequestParams;
import com.offroader.utils.CommonUtils;
import com.offroader.utils.LogUtils;
import com.offroader.utils.StringUtils;
import com.offroader.utils.ViewUtils;
import com.viphuli.app.R;
import com.viphuli.common.AccessTokenKeeper;
import com.viphuli.common.Constants;
import com.viphuli.http.ApiRequest;
import com.viphuli.http.bean.PageBaseBean;
import com.viphuli.util.ImageUtils;

/**
 * 
 * 护士加入
 * 
 * @author li.li
 *
 */
public class AccountNurseJoinFragment extends BaseProgressFragment {
	private static final int MAX_UPLOAD_IMG_COUNT = 2;

	@Bind(R.id.id_account_nurse_join_name)
	protected EditText nameText;

	@Bind(R.id.id_account_nurse_join_no)
	protected EditText noText;

	@Bind(R.id.id_account_nurse_join_hos)
	protected EditText hosText;

	@Bind(R.id.id_account_nurse_join_the_title)
	protected EditText theTitleText;

	@Bind(R.id.id_account_nurse_join_tel)
	protected EditText telText;

	@Bind({ R.id.id_account_nurse_join_upload_first, R.id.id_account_nurse_join_upload_second })
	protected List<ImageView> phtotUploadList;

	/** 已选择图片地址 */
	private List<String> selectedPhotos = new ArrayList<String>(MAX_UPLOAD_IMG_COUNT);

	private String phone;

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_account_nurse_join;
	}

	@Override
	protected void initParams() {
		//解析参数
		Bundle args = getArguments();
		phone = args.getString("phone");
	}

	@Override
	protected void initView() {

		if (StringUtils.isNotBlank(phone))
			telText.setText(phone);

	}

	@Override
	protected void initData() {

	}

	@Override
	public void onRequestResonse(PageBaseBean page) {
		if (isAdded())
			caller.finish();
	}

	@OnClick({ R.id.id_account_nurse_join_upload_first, R.id.id_account_nurse_join_upload_second })
	public void upload(View view) {
		CommonUtils.pickPhoto(getActivity(), MAX_UPLOAD_IMG_COUNT, Constants.ACTIVITY_REQUEST_PICK_PHOTO_CODE);
	}

	@OnClick(R.id.id_account_nurse_join_submit_btn)
	public void submit() {
		String name = nameText.getText().toString();
		String no = noText.getText().toString();
		String hos = hosText.getText().toString();
		String title = theTitleText.getText().toString();
		String tel = telText.getText().toString();

		if (StringUtils.isBlank(name)) {
			ViewUtils.toast(R.string.tip_input_name);
			return;
		}

		if (StringUtils.isBlank(no)) {
			ViewUtils.toast(R.string.tip_input_no);
			return;
		}

		if (StringUtils.isBlank(hos)) {
			ViewUtils.toast(R.string.tip_input_hos);
			return;
		}

		if (StringUtils.isBlank(title)) {
			ViewUtils.toast(R.string.tip_input_title);
			return;
		}

		if (StringUtils.isBlank(tel)) {
			ViewUtils.toast(R.string.tip_input_tel);
			return;
		}

		if (selectedPhotos == null || selectedPhotos.size() < 1) {
			ViewUtils.toast(R.string.tip_upload_certificate);
			return;
		}

		//网络请求
		AccessTokenKeeper token = AccessTokenKeeper.readAccessToken(getActivity());

		if (token.isLogin()) {

			RequestParams params = new RequestParams();
			params.put("uid", token.getOpenId());
			params.put("name", name);
			params.put("id_card", no);
			params.put("hospital", hos);
			params.put("title", title);
			params.put("tel", tel);

			try {

				File[] photos = new File[selectedPhotos.size()];
				for (int i = 0; i < selectedPhotos.size(); i++) {
					photos[i] = new File(selectedPhotos.get(i));
				}

				//图片文件数组
				params.put("photos[]", photos);

			} catch (FileNotFoundException e) {
				LogUtils.error(e.getMessage(), e);
				ViewUtils.toast(R.string.tip_upload_location_error);
				return;

			}

			ApiRequest.accountNurseJoin.request(this, params);

		} else {
			AccountLoginFragment.goWithConfirm(getActivity());
		}
	}

	//	/**
	//	 * 图片预览
	//	 * @param index
	//	 * @param selectedPhotos
	//	 */
	//	private void previewPhoto(int index, ArrayList<String> selectedPhotos) {
	//
	//		Intent intent = new Intent(getActivity(), PhotoPagerActivity.class);
	//		intent.putExtra(PhotoPagerActivity.EXTRA_CURRENT_ITEM, index);
	//		intent.putExtra(PhotoPagerActivity.EXTRA_PHOTOS, selectedPhotos);
	//		previewPhoto(intent);
	//
	//	}
	//
	//	/**
	//	 * 图片预览
	//	 * @param intent
	//	 */
	//	private void previewPhoto(Intent intent) {
	//		startActivityForResult(intent, Constants.ACTIVITY_REQUEST_PICK_PHOTO_CODE);
	//	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (resultCode == Activity.RESULT_OK && requestCode == Constants.ACTIVITY_REQUEST_PICK_PHOTO_CODE) {

			List<String> photos = null;//已选择的图片全路径
			if (data != null) {
				photos = data.getStringArrayListExtra(PhotoPickerActivity.KEY_SELECTED_PHOTOS);
			}

			//			if (photos == null || photos.size() != 2) {//2张图片
			//				ViewUtils.toast("请选择两张证件图片");
			//				return;
			//			}

			selectedPhotos.clear();
			if (photos != null && !photos.isEmpty()) {

				for (int i = 0; i < photos.size(); i++) {
					selectedPhotos.add(i, photos.get(i));
				}

			}
			
			for(View phtotUpload:phtotUploadList){
				phtotUpload.setVisibility(View.GONE);
			}

			for (int i = 0; i < selectedPhotos.size(); i++) {
				String imgUri = "file://" + selectedPhotos.get(i);
				ImageUtils.load(phtotUploadList.get(i), imgUri);

				phtotUploadList.get(i).setVisibility(View.VISIBLE);
			}

		}

	}

}
